package com.filepublic.demo.server.controller;

import com.filepublic.demo.common.annotations.PropertyNotNull;
import com.filepublic.demo.common.context.ThreadLocalContext;
import com.filepublic.demo.common.result.Result;
import com.filepublic.demo.pojo.dto.UserEnrollDto;
import com.filepublic.demo.pojo.dto.UserLoginDto;
import com.filepublic.demo.pojo.vo.UserVo;
import com.filepublic.demo.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户接口
 * Method：登录接口
 * 短信注册接口
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制器")
@Slf4j
public class UserController {

    @Resource
    UserService userService;


    /**
     * 登录
     */
    //发送来邮箱与密码，select * from email_user where user_name = "" and password = "";
    //todo 需要用户手动输入的，都必须进行非空检测
    @PropertyNotNull
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<UserVo> login(@RequestBody UserLoginDto userLoginDto){
        UserVo userVo = userService.login(userLoginDto);
        return Result.success(userVo);
    }

    /**
     * 注册
     */
    @PropertyNotNull
    @ApiOperation(value = "注册")
    @PostMapping("/enroll")
    public Result enroll(@RequestBody UserEnrollDto userEnrollDto){
        userService.enroll(userEnrollDto);
        return Result.success();
    }


    /**
     * 验证码
     */
    //接收邮箱值，生成验证码放入redis
    @PropertyNotNull
    @ApiOperation(value = "验证码")
    @GetMapping("/captcha/{emailUser}")
    public Result captcha(@PathVariable("emailUser") String emailUser){
        userService.captcha(emailUser);
        return Result.success();
    }

    /**
     * 注销
     * @param httpServletRequest
     * @return
     */
    @ApiOperation(value = "注销")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest httpServletRequest){
        userService.logout(httpServletRequest);
        return Result.success();
    }
}























